package com.tx.framework.datasource;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.tx.framework.entity.BaseEntity;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

public class BaseServiceImpl<T extends BaseEntity> implements BaseService<T> {

	@Autowired
	private BaseMapper<T> Mapper;

	/*@Override
	public boolean insert(T t) {
		int count = this.Mapper.insert(t);
		if (count == 1) {
			return true;
		}
		return false;
	}*/

	@Override
	public int deleteByPrimaryKey(T t) {
		return this.Mapper.deleteByPrimaryKey(t);
	}

	@Override
	public int delete(T t) {
		return this.Mapper.delete(t);
	}

	@Override
	public int insertSelective(T t) {
		return this.Mapper.insertSelective(t);
	}

	@Override
	public boolean existsWithPrimaryKey(T t) {
		return this.Mapper.existsWithPrimaryKey(t);
	}

	@Override
	public List<T> selectAll() {
		return this.Mapper.selectAll();
	}

	@Override
	public T selectByPrimaryKey(T t) {
		return this.Mapper.selectByPrimaryKey(t);
	}

	@Override
	public int selectCount(T t) {
		return this.Mapper.selectCount(t);
	}

	@Override
	public List<T> select(T t) {
		return this.Mapper.select(t);
	}

	@Override
	public T selectOne(T t) {
		return this.Mapper.selectOne(t);
	}

	/*@Override
	public int updateByPrimaryKey(T t) {
		return this.Mapper.updateByPrimaryKey(t);
	}*/

	@Override
	public int updateByPrimaryKeySelective(T t) {
		return this.Mapper.updateByPrimaryKeySelective(t);
	}

	@Override
	public int insertList(List<? extends T> list) {
		return this.Mapper.insertList(list);
	}

	@Override
	public List<T> selectByExample(Object obj) {
		return this.Mapper.selectByExample(obj);
	}

	@Override
	public T selectOneByExample(Object obj) {
		return this.Mapper.selectOneByExample(obj);
	}

	@Override
	public int updateByExampleSelective(T t, Object example) {
		return this.Mapper.updateByExampleSelective(t, example);
	}

	@Override
	public int deleteByExample(Object example) {
		return this.Mapper.deleteByExample(example);
	}

	/**
	 * 新增时判断用，返回字段对应的值在指定公司和系统公司存在的数据
	 */
	/*@Override
	public List<T> getListByTwoCompanys(Class<T> clas,Integer companyId,String fieldName,String value){
		Example example = new Example(clas);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo(fieldName, value);
		List<Integer> companyList = new ArrayList<Integer>();
		companyList.add(0);
		companyList.add(companyId);
		criteria.andIn("companyId", companyList);
		return this.selectByExample(example);
	}*/
	
	/**
	 * 编辑保存时判断用，返回字段对应的值与指定公司和系统公司存在，同时与指定的id不相等的数据
	 */
	/*@Override
	public List<T> getListByTwoCompanys(Class<T> clas,Integer companyId,String fieldName,String value,Integer notEqualId){
		Example example = new Example(clas);
		Example.Criteria criteria = example.createCriteria();
		criteria.andEqualTo(fieldName, value);
		List<Integer> companyList = new ArrayList<Integer>();
		companyList.add(0);
		companyList.add(companyId);
		criteria.andIn("companyId", companyList);
		criteria.andNotEqualTo("id", notEqualId);
		return this.selectByExample(example);
	}*/
	
	/**
	 * 查询列表数据时用，返回指定公司和系统公司的所有数据
	 */
	/*@Override
	public List<T> getListByTwoCompanys(Class<T> clas,Integer companyId){
		Example example = new Example(clas);
		Example.Criteria criteria = example.createCriteria();
		List<Integer> companyList = new ArrayList<Integer>();
		companyList.add(0);
		companyList.add(companyId);
		criteria.andIn("companyId", companyList);
		return this.selectByExample(example);
	}*/
	
	@Override
	public void updateByConditionSelective(T t,Map<String,Object> conditionMap){
		Example example = new Example(t.getClass());
		Example.Criteria criteria = example.createCriteria();
		for(Map.Entry<String, Object> entity: conditionMap.entrySet()){
			criteria.andEqualTo(entity.getKey(), entity.getValue());
		}
		this.Mapper.updateByExampleSelective(t, example);
	}
	
	@Override
	public Map<String,Object> getConditionMap(){
		Map<String,Object> map=new HashMap<>();
		return map;
	}
	
	@Override
	public Boolean addCheckIsExists(String fieldName,String fieldValue,Integer companyId,Class<T> clas){
		Example example = new Example(clas);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo(fieldName, fieldValue);
		criteria.andEqualTo("companyId", companyId);
		List<T> list=this.Mapper.selectByExample(example);
		if(list.size()>0){
			return true;
		}
		return false;
	}
	
	@Override
	public Boolean editCheckIsExists(Integer id,String fieldName,String fieldValue,Integer companyId,Class<T> clas){
		Example example = new Example(clas);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo(fieldName, fieldValue);
		criteria.andEqualTo("companyId", companyId);
		List<T> list=this.Mapper.selectByExample(example);
		if(list.size()>0){
			if(!list.get(0).getId().equals(id)){
				return true;
			}	
		}
		return false;
	}

	@Override
	public String getMoney(Integer money, Integer pointNumber) {
		BigDecimal total = new BigDecimal(String.valueOf(money));
		BigDecimal bdv = new BigDecimal(String.valueOf(100));
		return String.valueOf(total.divide(bdv,pointNumber,RoundingMode.HALF_UP));
	}
}
